﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using NHibernate;
using NHibernate.Cfg;
namespace Ecreate.Data.Hibernate
{
    public static class NHibernateDatabaseFactory
    {
        #region 私有静态变量

        private static object m_Locker = new object();
        private static Configuration m_Configuration = null;
        private static ISessionFactory m_SessionFactory = null;
        private static ISessionStorage m_Sessionsource;

        #endregion

        #region 静态构造函数

        static NHibernateDatabaseFactory()
        {
            m_Sessionsource = ISessionStorageFactory.GetSessionStorage();
        }

        #endregion

        #region 内部静态变量

        /**//// <summary>
        /// NHibernate配置对象
        /// </summary>
        public static Configuration Configuration
        {
            get
            {
                lock (m_Locker)
                {
                    if (m_Configuration == null)
                    {
                        CreateConfiguration();
                    }
                    return m_Configuration;
                }
            }
            set { m_Configuration = value; }
        }

        /**//// <summary>
        /// NHibernate的对象工厂
        /// </summary>
        public static ISessionFactory SessionFactory
        {
            get
            {
                if (null == m_SessionFactory)
                {
                    if (m_Configuration == null)
                    {
                        CreateConfiguration();
                    }
                    lock (m_Locker)
                    {
                        WebConfigAccess configAccess = new WebConfigAccess();
                        IDictionary<string, string> dictionary = new System.Collections.Generic.Dictionary<string, string>();
                        string hbmxml = System.Configuration.ConfigurationSettings.AppSettings["hbmxml"];
                        string mappingAssembly = System.Configuration.ConfigurationSettings.AppSettings["MappingAssembly"];
                        string popedomAssembly = System.Configuration.ConfigurationSettings.AppSettings["popedomAssembly"];
                        dictionary.Add("connection.connection_string", configAccess.ConnectionString);
                        
                        string hbmpath = Property.DataBaseConfigConfigHandler.getServerPath(hbmxml);
                        m_Configuration.Configure(hbmpath);
                        m_Configuration.AddAssembly(popedomAssembly);
                        m_Configuration.AddProperties(dictionary);
                        if (mappingAssembly == null) throw new Exception("Not Found MappingAssembly");
                        m_Configuration.AddAssembly(mappingAssembly);
                        m_SessionFactory = m_Configuration.BuildSessionFactory();
                    }
                }
                return m_SessionFactory;
            }
        }

        #endregion

        #region 公共方法

        /**//// <summary>
        /// 建立ISessionFactory的实例
        /// </summary>
        /// <returns></returns>
        public static ISession CreateSession()
        {
            if (Config.UserSessionSource) //如果使用保存的ISession
            {
                ISession s = m_Sessionsource.Get();
                if (s == null)
                {
                    s = SessionFactory.OpenSession();

                    m_Sessionsource.Set(s);
                }
                return s;
            }
            else //如果使用新ISession
            {
                return SessionFactory.OpenSession();
            }
        }

        #endregion

       #region 私有方法

        private static void CreateConfiguration()
        {
            m_Configuration = new Configuration();
            // Add interceptor, if you need to.
            // _config.Interceptor = new Interceptor();
        }

        #endregion
    }
}
